<?php
/**
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: Report.php 979 2011-10-31 07:00:36Z zldai $
 */
class IG_Report
{
    protected $_id;

    protected $_data;

    protected $_designs;

    protected $_formElements = array();

    protected $_view;

    /**
     * @var IG_Report_Abstract
     */
    static public $adapter;

    /**
     * IG_Report
     * @param array $config
     * @return IG_Report_Abstract
     */
    static public function factory($config = array(), $queryString = '', $params = null)
    {
        if ($config instanceof Zend_Config) {
            $config = $config->toArray();
        }

        if (!isset($config['type'])) {
            throw new Exception('Undefined data type!');
        }
        $adapter = $config['type'];
        $className = 'IG_Report_' . ucfirst($adapter);
        if (class_exists($className) && ($class = new $className($config['config'], $queryString, $params)) instanceof IG_Report_Abstract) {
            self::$adapter = $class;
            return $class;
        } else {
            throw new IG_Exception("'$className'方法不存在或不继承'IG_Report_Abstract'");
        }
    }

    /**
     *
     * @param array $data
     * @param Zend_Config|array $config
     */
    public function __construct($data, $config = array())
    {
        $this->_data = $data;
        if ($config instanceof Zend_Config) {
            $config = $config->toArray();
        }
        $designs = isset($config['designs'])?$config['designs']:array();
        if (!empty($designs)) {
            foreach ($designs as $key => $design) {
                $adapter = str_ireplace('design', '', $key);
                if($design) {
                    if($design instanceof Zend_Config) {
                        $design = $design->toArray();
                    } else if(is_string($design)) {
                        $design = (array)$design;
                    }
                } else {
                    $design = array();
                }

                $design['data'] = $this->_data;
                $this->_designs[] = IG_Report_Design::factory($adapter, $design);
            }
        } else {
            throw new Exception('No design for this report');
        }

        //获取查询参数
        if ($config['params']) {
            foreach ($config['params'] as $key => $param) {
                $dataType = $param['dataType'];
                unset($param['dataType']);
                unset($param['defaultValue']);
                $this->_formElements[] = $this->_createFormElement($dataType, $key, $param);
            }
            $this->_formElements[] = $this->_createFormElement('submit', '查询');
            $this->_formElements[] = $this->_createFormElement('submit', 'export');
        }
    }

    public function getId()
    {
        return $this->_id;
    }

    public function setId($id)
    {
        $this->_id = $id;
    }

    public function getFormElements()
    {
        return $this->_formElements;
    }

    public function display()
    {
        $html = '';
        foreach ($this->_designs as $design) {
            $html .= $design->render();
        }
        return $html;
    }

    private function _createFormElement($type, $id, $options = array())
    {
        $className = '';
        switch ($type){
            case 'datetime':
                $className = 'ZendX_JQuery_Form_Element_DatePicker';
                $options['jQueryParams'] = array('dateFormat' => 'yy-mm-dd');
                if ($options['value'] != '') $options['value'] = date('Y-m-d', strtotime($options['value']));
                break;
            case 'button':
            case 'text':
            case 'submit':
                $className = 'Zend_Form_Element_' . ucfirst($type);
                break;
            case 'select':
                $className = 'Zend_Form_Element_' . ucfirst($type);
                $multiOptions = $options['options'];
                unset($options['options']);
                $multi = array();
                $multi[''] = '请选择';
                if ($multiOptions['type'] == 'dynamic') {
                    $helper = $multiOptions['helper'];
                    $helperName = $helper['name'];
                    $data = $this->getView()->$helperName($helper['param']);
                    foreach ($data as $a1) {
                        $multi[$a1[$helper['key1']]] = $a1[$helper['key2']];
                    }
                } else {
                    foreach ($multiOptions['option'] as $arr) {
                        $multi[$arr['name']] = $arr['val'];
                    }
                }
                $options['multiOptions'] = $multi;
                break;
        }
        return new $className($id, $options);
    }

    /**
     *
     * @return Zend_View_Abstract
     */
    public function getView()
    {
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $this->setView($viewRenderer->view);
        return $this->_view;
    }

    public function setView($view)
    {
        $this->_view = $view;
        return $this;
    }
}